<template>
  <div class="list-table">
    <!--
    <div class="area-header">
      <span class="area-header-title">{{ $t('持仓明细') }}</span>
    </div>
    -->
    <el-form v-if="hideDiv" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('账户名称')" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          :placeholder="$t('账户名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('债券编号')" prop="bondCode">
        <el-input
          v-model="queryParams.bondCode"
          :placeholder="$t('债券编号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('债券名称')" prop="bondName">
        <el-input
          v-model="queryParams.bondName"
          :placeholder="$t('债券名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="hideDiv" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:positionInformation:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:positionInformation:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bondPositionDetailsList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('账户名称')" align="center" prop="accountName" width="220" />
      <el-table-column :label="$t('债券名称')" align="center" prop="bondName" width="150" />
      <el-table-column :label="$t('债券编号')" align="center" prop="bondCode" width="150" />
      <el-table-column :label="$t('持仓数量')" align="center" prop="numberPositions" width="100" />
      <el-table-column :label="$t('债券价格')" align="center" prop="bondPrice" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.bondPrice) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column :label="$t('债券久期')" align="center" prop="bondDuration" width="120" />-->
      <el-table-column :label="$t('债券收益率(%)')" align="center" prop="bondYield" width="110" />
      <el-table-column :label="$t('持仓市值')" align="center" prop="marketPositions" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.marketPositions) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('债券评级')" align="center" prop="bondRating" width="100" />
      <el-table-column :label="$t('主体评级')" align="center" prop="subjectRating" width="100" />
      <el-table-column :label="$t('持仓成本')" align="center" prop="holdingCost" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.holdingCost) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已质押份额')" align="center" prop="pledgedShare" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.pledgedShare) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('可质押份额')" align="center" prop="pledgeableShare" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.pledgeableShare) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="hideDiv" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:positionInformation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['deal:positionInformation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row,scope.$index)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div v-loading="loading">
      <el-divider />
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('评级分布') }}</span>
        </div>
        <bondRatingDistribution v-if="!loading" ref="bondRatingDistribution" :echarts-data-parent="pjfb" />
      </div>

      <el-divider />
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('区域分布') }}</span>
        </div>
        <bondRegionalDistribution v-if="!loading" ref="bondRegionalDistribution" :echarts-data-parent="qyfb" />
      </div>

      <el-divider />
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('剩余期限分布') }}</span>
        </div>
        <bondRemainingMaturityDistribution v-if="!loading" ref="bondRemainingMaturityDistribution" :echarts-data-parent="syqxfb" />
      </div>

      <el-divider />
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('收益率情况分布') }}</span>
        </div>
        <bondYieldDistribution v-if="!loading" ref="bondYieldDistribution" :echarts-data-parent="sylqkfb" />
      </div>
    </div>
    <!-- 添加或修改债券-持仓明细对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <bondPositionDetailsForm ref="bondPositionDetailsForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  // listBondPositionDetails,
  // delBondPositionDetails,
  exportBondPositionDetails
} from '@/api/bond/positionInformation/bondPositionDetails.js'
import bondPositionDetailsForm from './bondPositionDetailsForm.vue'
import bondRatingDistribution from '@/views/bond/bondPositionInformation/bondRatingDistribution.vue'
import bondRegionalDistribution from '@/views/bond/bondPositionInformation/bondRegionalDistribution.vue'
import bondRemainingMaturityDistribution from '@/views/bond/bondPositionInformation/bondRemainingMaturityDistribution.vue'
import bondYieldDistribution from '@/views/bond/bondPositionInformation/bondYieldDistribution.vue'
export default {
  components: {
    bondPositionDetailsForm,
    bondRatingDistribution,
    bondRegionalDistribution,
    bondRemainingMaturityDistribution,
    bondYieldDistribution
  },
  props: {
    hideDiv: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 债券-持仓明细表格数据
      bondPositionDetailsList: [],
      // 弹出层标题
      title: '债券-持仓明细',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: undefined,
        accountName: undefined,
        bondCode: undefined,
        bondName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pjfb: undefined,
      qyfb: undefined,
      syqxfb: undefined,
      sylqkfb: undefined
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询债券-持仓明细列表 */
    getList() {
      this.loading = true
      // listBondPositionDetails(this.queryParams).then(response => {
      //   this.bondPositionDetailsList = response.rows
      //   this.total = response.total
      //   this.loading = false
      // })
      setTimeout(() => {
        console.log(this.$route.params.panelType)
        if (this.$route.params.panelType === 'have') {
          // 自有资金
          this.bondPositionDetailsList = [
            { accountName: '通22转债', bondCode: '110085', bondName: '通22转债', bondPrice: '42.10', bondYield: '2.1', marketPositions: '1200000', bondRating: 'AA+', subjectRating: 'AA+', holdingCost: '54234', numberPositions: '5643', bondDuration: '', pledgedShare: '543.65', pledgeableShare: '274.87' },
            { accountName: '中环转2', bondCode: '123146', bondName: '中环转2', bondPrice: '6.87', bondYield: '3.0', marketPositions: '86400', bondRating: 'AA-', subjectRating: 'AA-', holdingCost: '13243', numberPositions: '13464', bondDuration: '', pledgedShare: '572.65', pledgeableShare: '176.87' },
            { accountName: '贵轮转债', bondCode: '127063', bondName: '贵轮转债', bondPrice: '4.25', bondYield: '2.0', marketPositions: '180000', bondRating: 'AA', subjectRating: 'AA', holdingCost: '24323', numberPositions: '76443', bondDuration: '', pledgedShare: '853.65', pledgeableShare: '3544.87' }
          ]
            this.pjfb = {
              titleZ: this.$t('金额'),
              titleX: this.$t('数量'),
              dataZ: [12.0, 13.3, 3.5, 3.7, 4.2, 7.2, 10.2, 3.4, 8.5, 5.1, 11.1, 5.6],
              dataX: [11.0, 12.2, 5.3, 6.5, 6.3, 8.2, 13.3, 2.4, 6.0, 7.5, 12.0, 6.2],
              optionsArr: ['AAA+', 'AAA', 'AAA-', 'AA+', 'AA', 'AA-', 'BBB+', 'BBB', 'BBB-', 'BB+', 'BB', 'BB-', 'B+', 'B', 'B-', 'CCC'],
              leftName: this.$t('债券余额(万元)'),
              rightName: this.$t('债券数量')
            }
            this.qyfb = {
              titleZ: this.$t('金额'),
              titleX: this.$t('数量'),
              dataZ: [0, 1.0, 1.5, 2.0, 2.4, 3.3, 4.2, 4.5, 6.7, 9, 12.0, 15.0],
              dataX: [0, 1, 1, 1, 2, 3, 3, 4, 5, 5, 8, 9],
              optionsArr: ['北京', '上海', '天津', '河南', '湖北', '安徽', '山东', '福建', '河北', '辽宁', '广东', '江苏'],
              leftName: this.$t('债券余额(万元)'),
              rightName: this.$t('债券数量')
            }
            this.syqxfb = {
              titleZ: this.$t('金额'),
              titleX: this.$t('数量'),
              dataZ: [1.5, 3, 5.53, 9.1, 0.78],
              dataX: [4, 6, 13, 24, 6],
              optionsArr: ['1年以下', '1-3年', '3-5年', '5-7年', '7年以上'],
              leftName: this.$t('债券余额(万元)'),
              rightName: this.$t('债券数量')
            }
            this.sylqkfb = {
              titleZ: this.$t('金额'),
              titleX: this.$t('数量'),
              dataZ: [1.43, 1.28, 8.96, 6.85, 1.4],
              dataX: [6, 5, 19, 20, 3],
              optionsArr: ['4%以下', '4%(含)-5%', '5%(含)-6%', '6%(含)-7%', '7%以上'],
              leftName: this.$t('债券余额(万元)'),
              rightName: this.$t('债券数量')
            }
        } else {
          // 基金资金
          this.bondPositionDetailsList = [
            { accountName: '药石转债', bondCode: '123146', bondName: '药石转债', bondPrice: '83.11', bondYield: '2.1', marketPositions: '115000', bondRating: 'AA', subjectRating: 'AA', holdingCost: '54234', numberPositions: '5643', bondDuration: '', pledgedShare: '543.65', pledgeableShare: '274.87' },
            { accountName: '精工转债', bondCode: '110086', bondName: '精工转债', bondPrice: '4.5', bondYield: '2.0', marketPositions: '200000', bondRating: 'AA', subjectRating: 'AA', holdingCost: '24323', numberPositions: '76443', bondDuration: '', pledgedShare: '853.65', pledgeableShare: '3544.87' }
          ]
          this.pjfb = undefined
          this.qyfb = undefined
          this.syqxfb = undefined
          this.sylqkfb = undefined
        }
        this.loading = false
      }, 200)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        accountName: undefined,
        bondCode: undefined,
        bondName: undefined,
        numberPositions: undefined,
        bondPrice: undefined,
        bondDuration: undefined,
        bondYield: undefined,
        marketPositions: undefined,
        bondRating: undefined,
        subjectRating: undefined,
        holdingCost: undefined,
        pledgedShare: undefined,
        pledgeableShare: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.bondPositionDetailsForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.bondPositionDetailsForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row, index) {
      var _this = this
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        // return delBondPositionDetails(row.id)
        console.log(row)
        _this.bondPositionDetailsList.splice(index, 1)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportBondPositionDetails(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
